//
// Created by francklinson on 2021/5/25.
//

#include <vector>
#include <iostream>

using namespace std;

//Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
    TreeNode *mirrorTree(TreeNode *root) {
        if (root == nullptr) return nullptr;
        auto node = new TreeNode(root->val);
        node->left = mirrorTree(root->right);
        node->right = mirrorTree(root->left);
        return node;
    }

    void preorder(TreeNode *root, vector<int> &res) {
        if (root == nullptr) {
            return;
        }
        res.push_back(root->val);
        preorder(root->left, res); // 对左子节点递归调用
        preorder(root->right, res); // 对右子节点递归调用
    }

    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> res; // 结果向量
        preorder(root, res);
        return res;
    }
};

int main() {
    auto n1 = TreeNode(4), n2 = TreeNode(2), n3 = TreeNode(7), n4 = TreeNode(1), \
 n5 = TreeNode(3), n6 = TreeNode(6), n7 = TreeNode(9);
    n1.left = &n2;
    n1.right = &n3;
    n2.left = &n4;
    n2.right = &n5;
    n3.left = &n6;
    n3.right = &n7;
    Solution sol;
    auto ans = sol.mirrorTree(&n1);
    auto pre = sol.preorderTraversal(&n1);
    for (auto &x:pre) cout << x << endl;
    cout << "------------" <<endl;
    pre = sol.preorderTraversal(ans);
    for (auto &x:pre) cout << x << endl;
    return 0;
}